package org.liang.system.service.impl;

import org.liang.base.service.impl.BaseServiceImpl;
import org.liang.system.domain.Department;
import org.liang.system.mapper.DepartmentMapper;
import org.liang.system.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void insert(Department department) {
        departmentMapper.insert(department);
        //添加path路径
        if(Objects.isNull(department.getParent().getId())){
            //说明当前没有上级部门
            department.setPath("/"+department.getId());
        }else {
            Department department1 = departmentMapper.selectById(department.getParent().getId());
            department.setPath(department1.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        //添加path路径
        if(Objects.isNull(department.getParent().getId())){
            //说明当前没有上级部门
            department.setPath("/"+department.getId());
        }else {
            Department department1 = departmentMapper.selectById(department.getParent().getId());
            department.setPath(department1.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public List<Department> deptTree() {
        return departmentMapper.deptTree();
    }

    @Override
    public List<Department> deptTreeTh() {
        return departmentMapper.deptTreeTh();
    }

}
